<h1>添加新文章</h1>

<% form_for([:admin , @article] ,:url => admin_admin_articles_path,:html=>{:class => "form",:multipart => true}) do |f| %>
  <%= f.error_messages %>

  <table class="table">
    <tbody>
    <tr class="odd">
      <td width="60px">标题</td>
      <td width="200px">    <%= f.text_field :title %></td>
    </tr>
    <tr>
      <td width="60px">幅标题</td>
      <td width="200px">     <%= f.text_field :subhead %></td>
      <td width=""> </td>
    </tr>
    <tr>
      <td width="60px">来源</td>
      <td width="200px">     <%= f.text_field :from %></td>
      <td width=""> </td>
    </tr>
    <tr>
      <td width="60px">分类</td>
      <td width="200px">    <%= f.select :category_id ,Category.all.map{|c| [c.name,c.id]} %></td>
      <td width=""> </td>
    </tr>
        <tr>
      <td width="60px">模块</td>
      <td width="200px">    <%= f.select :status ,Article::Status.each_with_index{|s , i| [s,i]} %></td>
      <td width=""> </td>
    </tr>
    <tr>
      <td width="60px">作者</td>
      <td width="200px">     <%= f.text_field :author %></td>
      <td width=""> </td>
    </tr>
    <tr>
      <td width="60px">简介</td>
      <td width="200px">   <%= f.text_field :summary %></td>
      <td width=""> </td>
    </tr>
  
    <tr>
      <td width="60px">内容</td>
      <td width="200px"><%= f.text_area :body %></td>
      <td width=""> </td>
    </tr>
    <tr>
      <td width="60px">选择图像</td>
      <td width="200px">  <p>
        <%=  file_field_tag(:image) %>
        <% unless @article.article_images.blank? %>
          <% @article.article_images.each do |picture| %>
            <div class="file_list" id="picture_< %= picture.id %>">
              <ul>
                <li>
                  文件: <%= picture.image_file_name %>
                  <%= link_to_remote "删除图片",:url=>delete_product_picture_ajax_path(:p_id=>picture.id),:confirm=>"是否要删除该产品图片?",:method=>:get%>
                </li>
              </ul>
            </div>
          <% end %>
        <% end %>
      </p></td>
    </tr>
    <tr>
      <td width="60px"></td>
      <td width="200px">   <%= f.submit 'Create'  ,:class => "button" %></td>
      <td width=""> </td>
    </tr>
  </table>
<% end %>

<%= link_to '返回', admin_admin_articles_path %>


<script type="text/javascript">
  var multiple_upload_attachment_counter = 0;
  multiple_upload_attachment($("image"), 100);
  function multiple_upload_attachment(ele,max_size)
  {
    Event.observe(ele,'change',function(){
      addAttachmentDiv(ele,max_size);
    }
  );
    if(multiple_upload_attachment_counter>=max_size)ele.disabled=true;
  }
  function addAttachmentDiv(ele, max_size)
  {
    if(!$A(['jpg','bmp','png','gif','rar','zip','tar','gz','jar','war']).any(function(extName){return new RegExp('\\.'+extName+'$','i').test(ele.value);})){
      alert("文件类型错误！");
      return false;
    }
    multiple_upload_attachment_counter++;
    var div=new Element("div").update("<ul><li>文件: "+ele.value+" <a href='#' onclick='removeAttachmentDiv(this, \""+ele.id+"\");return false;'>删除</a></li></ul>");
    div.addClassName("file_list")
    var new_input=new Element("input",{type:"file",name:ele.name,id:ele.id,disabled:multiple_upload_attachment_counter>=max_size});
    Event.observe(new_input,'change',function(){
      addAttachmentDiv(new_input,max_size);
    }
  );
    ele.insert({before:new_input,after:div});
    ele.id=ele.id+multiple_upload_attachment_counter;
    ele.name="image[]";
    div.appendChild(ele.hide().remove());
  }
  function removeAttachmentDiv(link,eleId){
    multiple_upload_attachment_counter--;
    link.parentNode.parentNode.parentNode.remove();
    $(eleId).disabled=false;
  }
</script>    